package com.example.crm01.service.impl.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.crm01.entity.Business;
import com.example.crm01.entity.system.Role;
import com.example.crm01.mapper.system.RoleMapper;
import com.example.crm01.service.RoleService;
import com.example.crm01.service.form.BaseIdForm;
import com.example.crm01.service.form.system.RoleAddForm;
import com.example.crm01.service.form.system.RoleListForm;
import com.example.crm01.utils.StringUtils;
import jdk.nashorn.internal.codegen.CompilerConstants;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;


@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    public final RoleMapper roleMapper;

    public RoleServiceImpl(RoleMapper roleMapper) {
        this.roleMapper = roleMapper;
    }

    @Override
    public Page<Role> listRole(RoleListForm form) {
        Page<Role> page = new Page<>(form.getPageNum(), form.getPageSize());

        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(form.getRoleName())) {
            queryWrapper.like(Role::getRoleName, form.getRoleName());
        }
        if (ObjectUtils.isNotEmpty(form.getId())) {
            queryWrapper.eq(Role::getId, form.getId());
        }

        queryWrapper.ne(Role::getDel_flag, 2);
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public Boolean addRole(RoleAddForm form) {
        Role role = new Role();
        role.setRoleName(form.getRoleName());
        if (StringUtils.isNotEmpty(form.getRoleRemark())){
            role.setRoleRemark(form.getRoleRemark());
        }

        //-----
        role.setCreateBy("1");
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateBy("1");
        role.setUpdateTime(LocalDateTime.now());
        role.setDel_flag(0);
        int insert = roleMapper.insert(role);
        if (insert > 0) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    @Override
    public Boolean updateRole(RoleAddForm form) {
        if (ObjectUtils.isEmpty(form.getId())){
            throw new RuntimeException("修改角色信息，主键id不能为空");
        }

        Role role = roleMapper.selectById(form.getId());
        role.setRoleName(form.getRoleName());
        if (StringUtils.isNotEmpty(form.getRoleRemark())){
            role.setRoleRemark(form.getRoleRemark());
        }

        //-----
        role.setUpdateBy("1");
        role.setUpdateTime(LocalDateTime.now());
        role.setDel_flag(1);
        int insert = roleMapper.updateById(role);
        if (insert > 0) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;


    }

    @Override
    public Boolean deleteRole(BaseIdForm form) {
        Role role = roleMapper.selectById(form.getId());
        if (ObjectUtils.isEmpty(role)) {
            throw new RuntimeException("角色信息不存在删除失败");
        }
        role.setUpdateBy("1");
        role.setUpdateTime(LocalDateTime.now());
        role.setDel_flag(2);
        int update = baseMapper.updateById(role);
        if (update > 0) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }
}
